<?php
namespace app\models;
use app\modules\company\models\Company;

use Yii;

class CompanyAccountRecord extends Basic
{
    public static function tableName()
    {
        return '{{%company_account_record}}';
    }

    public function rules()
    {
        return [
            ['comid', 'required', 'message' => 'comid不能为空'],
            ['comid', 'string', 'message' => 'comid必须是字符串', 
                      'min' => 8, 'tooShort' => 'comid最短8位', 
                      'max' => 8, 'tooLong' => 'comid最长8位'],
            ['appid', 'default', 'value' => ''],
            ['appid', 'string', 'message' => 'appid必须是字符串',
                      'min' => 18, 'tooShort' => 'appid最短18位',
                      'max' => 18, 'tooLong' => 'appid最长18位'],
            ['flag', 'integer', 'message' => 'flag必须为数字，或数字字符串'],
            ['flag', 'in', 'range' => array(1, 2, 3), 'message' => 'flag值必须在1-3中选择'],
            ['year', 'integer', 'message' => 'year必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'year最小存储0', 
                        'max'=>99, 'tooBig' => 'year最大存储99'],
            ['amount', 'integer', 'message' => 'amount必须为数字，或数字字符串', 
                        'min'=>1, 'tooSmall' => 'amount最小存储1', 
                        'max'=>4294967295, 'tooBig' => 'amount最大存储4294967295'],
            ['paymode', 'integer', 'message' => 'paymode必须为数字，或数字字符串'],
            ['paymode', 'in', 'range' => array(1, 2, 3), 'message' => 'paymode值必须在1-3中选择'],
            ['creater', 'default', 'value' => 'system', 'on' => 'add'],
            ['creater', 'string', 'message' => 'creater必须是字符串', 
                        'max' => 16, 'tooLong' => 'creater最长16位'],
            ['remark', 'default', 'value' => 'system', 'on' => 'add'],
            ['remark', 'string', 'message' => 'remark必须是字符串', 
                        'max' => 256, 'tooLong' => 'remark最长256位'],
            ['isdel', 'integer', 'message' => 'isdel必须为数字，或数字字符串'],
            ['isdel', 'in', 'range' => array(1, 2), 'message' => 'isdel值必须在1-2中选择'],
            ['add_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['add_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'add_time非yyyy-MM-dd HH:mm:ss格式'],
        ];
    }

    /*添加记录*/
    public function addRecord($data)
    {
        // P($data);
        $data['CompanyAccountRecord']['creater'] = $this->userInfo['username'];
        $data['CompanyAccountRecord']['add_time'] = date('Y-m-d H:i:s', time());
        // P($data);
        if($this->load($data) and $this->validate()){
            if($this->save(false)){
                return $this->amount;
            }
        }
        return false;
    }

    /*关联查询 企业信息*/
    public function getCompany()
    {
        $company = $this->hasOne(Company::className(), ['comid' => 'comid'])
                    ->select(['comid', 'company_name']);
        return $company;
    }


}